//
//  HNSH_OrderDetailBottomView.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/1.
//

#import "HNSH_OrderDetailBottomView.h"

@interface HNSH_OrderDetailBottomView ()

/** 背景 */
@property (nonatomic, strong)UIView * bgView;
/** 标题 */
@property (nonatomic, strong)UILabel * titleLabel;
/** 状态 */
@property (nonatomic, strong)UILabel * amountLabel;

@property (nonatomic, strong)UIButton * firBtn;
/**  */
@property (nonatomic, strong)UIButton * secBtn;
/** 删除订单 */
@property (nonatomic, strong)UIButton * thrBtn;

@end

@implementation HNSH_OrderDetailBottomView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self configUI];
    }
    return self;
}

- (void)configUI {
    [self addAllViews];
    [self addAllLayout];
}

//MARK: Cycle Methods
- (void)addAllViews{
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.titleLabel];
    [self.bgView addSubview:self.amountLabel];
    [self.bgView addSubview:self.firBtn];
    [self.bgView addSubview:self.secBtn];
    [self.bgView addSubview:self.thrBtn];
}

- (void)addAllLayout{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(55);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(9);
        make.height.mas_equalTo(17);
        make.left.mas_equalTo(16);
    }];
    [self.amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(1);
        make.height.mas_equalTo(20);
        make.left.mas_equalTo(16);
    }];
    [self.firBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(10);
        make.width.mas_equalTo(107);
        make.height.mas_equalTo(35);
    }];
    [self.secBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.firBtn.mas_left).offset(-7);
        make.top.width.height.mas_equalTo(self.firBtn);
    }];
    [self.thrBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.secBtn.mas_left).offset(-7);
        make.top.width.height.mas_equalTo(self.firBtn);
    }];
}

- (void)btnAction:(UIButton *)sender{
    if (self.clickBtnBlock) {
        self.clickBtnBlock(sender);
    }
}

//MARK: Setter Methods
- (void)setModel:(HNSH_OrderDetailModel *)model{
    _model = model;
    
    //20:发货中 30:待收货 100:已完成 110:已取消 200:已退款
    if (model.order_status == 20 ||
        model.order_status == 30 ||
        model.order_status == 100 ||
        model.order_status == 110 ||
        model.order_status == 200 ) {
        self.titleLabel.hidden = YES;
        self.amountLabel.hidden = YES;
    } else {
        self.titleLabel.hidden = NO;
        self.amountLabel.hidden = NO;
        NSAttributedString * attString = [[NSAttributedString alloc] initWithData:[model.summary_price_text dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
        self.amountLabel.attributedText = attString;
    }
    [self changeLayoutWithWithModel:model];
}

- (void)changeLayoutWithWithModel:(HNSH_OrderDetailModel *)model{
    NSArray * sortArray = [[model.buttons reverseObjectEnumerator] allObjects];
    if (sortArray.count > 0) {
        self.firBtn.hidden = NO;
        self.secBtn.hidden = YES;
        self.thrBtn.hidden = YES;
        HNSH_OrderButtonModel * btnModel = sortArray[0];
        [self.firBtn setTitle:btnModel.name forState:UIControlStateNormal];
        if ([btnModel.style isEqualToString:@"highlight"]) {
            self.firBtn.backgroundColor = [UIColor appGreenColor];
            [self.firBtn setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        } else {
            self.firBtn.backgroundColor = [UIColor appBGColor];
            [self.firBtn setTitleColor:[UIColor appBlackLabelColor] forState:UIControlStateNormal];
        }
    }
    if (sortArray.count > 1) {
        self.firBtn.hidden = NO;
        self.secBtn.hidden = NO;
        self.thrBtn.hidden = YES;
        HNSH_OrderButtonModel * btnModel = sortArray[1];
        [self.secBtn setTitle:btnModel.name forState:UIControlStateNormal];
        if ([btnModel.style isEqualToString:@"highlight"]) {
            self.secBtn.backgroundColor = [UIColor appGreenColor];;
            [self.secBtn setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        } else {
            self.secBtn.backgroundColor = [UIColor appBGColor];
            [self.secBtn setTitleColor:[UIColor appBlackLabelColor] forState:UIControlStateNormal];
        }
    }
    if (sortArray.count > 2) {
        self.firBtn.hidden = NO;
        self.secBtn.hidden = NO;
        self.thrBtn.hidden = NO;
        HNSH_OrderButtonModel * btnModel = sortArray[2];
        [self.thrBtn setTitle:btnModel.name forState:UIControlStateNormal];
        if ([btnModel.style isEqualToString:@"highlight"]) {
            self.thrBtn.backgroundColor = [UIColor appGreenColor];;
            [self.thrBtn setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        } else {
            self.thrBtn.backgroundColor = [UIColor appBGColor];
            [self.thrBtn setTitleColor:[UIColor appBlackLabelColor] forState:UIControlStateNormal];
        }
    }
}


//MARK: Getter Methods
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor appGrayLabelColor];
        _titleLabel.font = [UIFont systemFontOfSize:12];
        _titleLabel.text = @"应付金额：";
    }
    return _titleLabel;
}

- (UILabel *)amountLabel{
    if (!_amountLabel) {
        _amountLabel = [UILabel new];
        _amountLabel.textColor = [UIColor appBlackLabelColor];
        _amountLabel.font = [UIFont boldSystemFontOfSize:14];
    }
    return _amountLabel;
}

- (UIButton *)firBtn{
    if (!_firBtn) {
        _firBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _firBtn.layer.cornerRadius = 10;
        _firBtn.layer.masksToBounds = YES;
        [_firBtn setTitle:@"立即付款" forState:UIControlStateNormal];
        [_firBtn setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        _firBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _firBtn.backgroundColor = [UIColor appGreenColor];
        [_firBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        _firBtn.hidden = YES;
    }
    return _firBtn;
}

- (UIButton *)secBtn{
    if (!_secBtn) {
        _secBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _secBtn.layer.cornerRadius = 10;
        _secBtn.layer.masksToBounds = YES;
        [_secBtn setTitle:@"取消订单" forState:UIControlStateNormal];
        [_secBtn setTitleColor:[UIColor appBlackLabelColor] forState:UIControlStateNormal];
        _secBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _secBtn.backgroundColor = [UIColor appBGColor];
        [_secBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        _secBtn.hidden = YES;
    }
    return _secBtn;
}

- (UIButton *)thrBtn{
    if (!_thrBtn) {
        _thrBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _thrBtn.layer.cornerRadius = 10;
        _thrBtn.layer.masksToBounds = YES;
        [_thrBtn setTitle:@"删除订单" forState:UIControlStateNormal];
        [_thrBtn setTitleColor:[UIColor appBlackLabelColor] forState:UIControlStateNormal];
        _thrBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _thrBtn.backgroundColor = [UIColor appBGColor];
        [_thrBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        _thrBtn.hidden = YES;
    }
    return _thrBtn;
}

@end
